﻿using System.Collections;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml.Serialization;
using PDO.DataPersistence.Core;
using PDO.Utilities;

using System.Text;

namespace PDO.UI.Core
{
    /// <summary>
    /// Base class for Presentation Models.
    /// </summary>
    public class BasePM : PersistableDataObjectRoot, IPersistableDataObjectOwner
    {

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public BasePM()
        {
//            _history = new UndoRedoHistory<IPersistableDataObjectOwner>(this);
        }

        #endregion Constructor

        #region Public Properties


        /// <summary>
        /// Touch a PersistableDataObject to force lazy-loading.
        /// </summary>
        /// <param name="pdo"></param>
        public void Touch(PersistableDataObject pdo)
        {
            // no-op method used to force laz-loading of properties.
        }


      

        ContentControl _hostControl;

        [XmlIgnore]
        public ContentControl HostControl
        {
            get { return _hostControl as ContentControl; }
            set
            {
                _hostControl = value; // set value on base class
            }
        }

        private bool _isReadOnly;
        public bool IsReadOnly
        {
            get { return _isReadOnly; }
            set { SetBool(ref _isReadOnly, value, () => this.IsReadOnly); }
        }

        private string _notificationMessage;

        [XmlIgnore]
        public string NotificationMessage
        {
            get { return _notificationMessage; }
            set { SetString(ref _notificationMessage, value, () => this.NotificationMessage); }
        }


      


        private PersistableDataObject _pdoDataContext;

        [XmlIgnore]
        public PersistableDataObject PDODataContext
        {
            get { return _pdoDataContext; }
            set { SetPDO(ref _pdoDataContext, value, ()=>this.PDODataContext); }
        }

        #endregion Public Properties

        #region PersistableDataObject overrides

        protected override bool Insert()
        {
            // A PM has no directly persistable data properties, only child PersistableDataObjects.
            return true; // do nothing 
        }

        protected override bool Update()
        {
            // A PM has no directly persistable data properties, only child PersistableDataObjects.
            return true; // do nothing 
        }

        #endregion

        #region CollectionViewSource related helper functions

        /// <summary>
        /// Return currently selected item from list when list has been wrapped 
        /// in a CollectionViewSource.
        /// </summary>
        /// <param name="list">wrapped list</param>
        /// <returns>selected item</returns>
        protected object GetCurrentListItem(IList list)
        {
            if (null != list)
            {
                return CollectionViewSource.GetDefaultView(list).CurrentItem;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Set the current item in the assoicated View to the specified object.
        /// Note that for a bound control, e.g. a ListBox, the control will visually show 
        /// the new current item selection, but the control may not scroll the newly 
        /// current item into view.
        /// </summary>
        /// <param name="list">data source list</param>
        /// <param name="item">item to make current</param>
        protected void SetCurrentListItem(IList list, object item)
        {
            if (null != list)
            {
                CollectionViewSource.GetDefaultView(list).MoveCurrentTo(item);
            }
        }

        #endregion

        #region [UNDO REDO]

        //private UndoRedoHistory<IPersistableDataObjectOwner> _history = null;

        ///// <summary>
        ///// Saves a copy of the bound object.
        ///// </summary>
        //public void Do()
        //{
        //    _history.Do(new PersistableDataObjectMemento(this.PDODataContext));
        //}

        //public bool CanUndo()
        //{
        //    return _history.CanUndo;
        //}

        //public bool CanRedo()
        //{
        //    return _history.CanRedo;
        //}

        ///// <summary>
        ///// Undoes this bound object.
        ///// </summary>
        //public void Undo()
        //{
        //    if (_history.CanUndo)
        //        _history.Undo();
        //}

        ///// <summary>
        ///// Redoes this bound object.
        ///// </summary>
        //public void Redo()
        //{
        //    if (_history.CanRedo)
        //        _history.Redo();
        //}

        #endregion UNDO REDO

    }


    #region [UNDO REDO Interface and Related Class]
    interface IPersistableDataObjectOwner
    {
        PersistableDataObject PDODataContext
        {
            get;
            set;
        }
    }

    
    #endregion [UNDO REDO Interface and Related Class]
}
